package com.powernode.model;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class R<T> {

    private Integer code;
    private String msg;
    private T data;

    /**
     * 操作成功
     * @param data
     * @return
     * @param <T>
     */
    public static <T> R<T> OK(T data) {
        return R.<T>builder()
                .code(CodeEnum.SUCCESS.getCode())
                .msg(CodeEnum.SUCCESS.getDesc())
                .data(data)
                .build();
    }

    /**
     * 操作失败
     * @return
     * @param <T>
     */
    public static <T> R<T> FAIL(CodeEnum codeEnum) {
        return R.<T>builder()
                .code(codeEnum.getCode())
                .msg(codeEnum.getDesc())
                .data(null)
                .build();
    }

    /**
     * 操作失败
     * @return
     * @param <T>
     */
    public static <T> R<T> FAIL(String message) {
        return R.<T>builder()
                .code(CodeEnum.ERROR.getCode())
                .msg(message)
                .data(null)
                .build();
    }

    /**
     * 操作成功
     * @param data
     * @return
     * @param <T>
     */
    public static <T> R<T> HANDLER(Boolean flag) {
        if (flag) {
            return R.<T>builder()
                    .code(CodeEnum.SUCCESS.getCode())
                    .msg(CodeEnum.SUCCESS.getDesc())
                    .data(null)
                    .build();
        }
        return R.<T>builder()
                .code(CodeEnum.ERROR.getCode())
                .msg(CodeEnum.ERROR.getDesc())
                .data(null)
                .build();
    }
}
